package com.gykfbxxh.imovie.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioGroup;

import java.util.List;

/**
 * 管理fragment的工具
 * @author xuxinghai
 *
 */
public class MainFragmentTabAdapter {

	private List<Fragment> fragments;
	private int count;
	private FragmentManager fragmentManager; // Fragment所属的Activity
	private int fragmentContentId; // Activity中所要被替换的区域的id
	private int currentTab; // 当前Tab页面索引
	private OnRgsExtraCheckedChangedListener onRgsExtraCheckedChangedListener; // 用于让调用者在切换tab时候增加新的功能

	public MainFragmentTabAdapter(FragmentManager fragmentManager, List<Fragment> fragments, int fragmentContentId, int count) {
			this.fragments = fragments;
			this.count=count;
			this.fragmentManager = fragmentManager;
			this.fragmentContentId = fragmentContentId;
		// 默认显示第一页
//		FragmentTransaction ft = fragmentManager.beginTransaction();
//		ft.replace(fragmentContentId,fragments.get(0));
//		ft.commit();
		if (!fragments.get(0).isAdded()) {
			FragmentTransaction ft = fragmentManager.beginTransaction();
			ft.add(fragmentContentId, fragments.get(0));
			ft.commitAllowingStateLoss();
		}
	}

	public void onChanged(int idx) {
		if(currentTab == idx) return;
		for (int i = 0; i < count; i++) {
			if(idx==i){
				Fragment fragment = fragments.get(i);
				FragmentTransaction ft = obtainFragmentTransaction(i);
//				for (int j = 0; j < fragments.size(); j++) {
//					fragmentManager.
//				}
				getCurrentFragment().onStop(); // 暂停当前tab
				if (fragment.isAdded()) {
					fragment.onStart(); // 启动目标tab的onStart()
				} else {
					ft.add(fragmentContentId, fragment);
				}
				showTab(i); // 显示目标tab
				ft.commitAllowingStateLoss();

//				// 如果设置了切换tab额外功能功能接口
//				if (null != onRgsExtraCheckedChangedListener) {
//					onRgsExtraCheckedChangedListener.OnRgsExtraCheckedChanged(radioGroup, checkedId, i);
//				}
			}
		}
	}

	/**
	 * 切换tab
	 * 
	 * @param idx
	 */
	private void showTab(int idx) {
		for (int i = 0; i < fragments.size(); i++) {
			Fragment fragment = fragments.get(i);
			FragmentTransaction ft = obtainFragmentTransaction(idx);
			if (idx == i) {
				ft.show(fragment);
			} else {
//				ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
				ft.hide(fragment);
			}
			ft.commitAllowingStateLoss();
		}
		currentTab = idx; // 更新目标tab为当前tab
	}

	/**
	 * 获取一个带动画的FragmentTransaction
	 * 
	 * @param index
	 * @return
	 */
	private FragmentTransaction obtainFragmentTransaction(int index) {
		FragmentTransaction ft = fragmentManager.beginTransaction();
		// 设置切换动画
//		if (index > currentTab) {
//			ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out, android.R.anim.fade_in, android.R.anim.fade_out);
////			ft.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left);
//		} else {
////			ft.setCustomAnimations(0, R.anim.slide_out_right);
//			ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out, android.R.anim.fade_in, android.R.anim.fade_out);
//		}
		return ft;
	}

	public int getCurrentTab() {
		return currentTab;
	}

	public Fragment getCurrentFragment() {
		return fragments.get(currentTab);
	}

	public OnRgsExtraCheckedChangedListener getOnRgsExtraCheckedChangedListener() {
		return onRgsExtraCheckedChangedListener;
	}

	public void setOnRgsExtraCheckedChangedListener(OnRgsExtraCheckedChangedListener onRgsExtraCheckedChangedListener) {
		this.onRgsExtraCheckedChangedListener = onRgsExtraCheckedChangedListener;
	}

	/**
	 * 切换tab额外功能功能接口
	 */
	public static interface OnRgsExtraCheckedChangedListener {
		public void OnRgsExtraCheckedChanged(RadioGroup radioGroup, int checkedId, int index);
	}
	
}
